package com.situ.ftbserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.situ.ftbserver.mapper.CourseMapper;
import com.situ.ftbserver.model.Course;
import com.situ.ftbserver.model.search.CourseSearchBean;
import com.situ.ftbserver.service.CourseService;
import com.situ.ftbserver.util.PaginateInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@CacheConfig(cacheNames = "courseServiceImpl")
@Service
public class CourseServiceImpl implements CourseService {
    private CourseMapper courseMapper;
    @Autowired
    public void setCourseMapper(CourseMapper courseMapper) {
        this.courseMapper = courseMapper;
    }

    @Override
    public List<Course> findAll(PaginateInfo pi, CourseSearchBean csb) {

        try(Page<?> p = PageHelper.startPage(pi.getPageNo(), pi.getPageSize())){
            return courseMapper.findAll(csb);
        }
    }

    @CacheEvict(allEntries = true)
    @Override
    public int delete(Integer[] ids) {
        return courseMapper.delete(ids);
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean save(Course course) {
        if(courseMapper.insert(course)>0){
            return true;
        }
        return false;
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean update(Course course) {
        return courseMapper.updateById(course)>0;
    }

    @Cacheable(key = "'getCourse'")
    @Override
    public List<Course> getCourse() {
        return courseMapper.selectList( null);
    }

    @Override
    public List<Course> findByCoachId(Integer[] ids) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("coach_id", ids);
        return courseMapper.selectList(queryWrapper);
    }

    @Override
    public List<Map<String, Object>> getPopularCourse() {
        return courseMapper.getPopularCourse();
    }

}
